﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    public partial class TableDataExtractorEditor : Form, IComponentEditor
    {
        private object _ActivityNode;
        private TableDataExtractorComponent _ExtractComponent;
        /// <summary>
        /// 
        /// </summary>
        public TableDataExtractorEditor()
        {
            InitializeComponent();
        }

        private void btn_BroseDocFolder_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_FileListXmlFilePath.Text = dlg.FileName;
            }
        }

        private void btn_BrowseScanOutputFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_LayoutFilePath.Text = dlg.FileName;
            }
        }

        private void btn_SaveOutputTo_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_OutputFolder.Text = dlg.SelectedPath;
            }
        }

        private void btn_BrowseLog_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Log files(*.log)|*.log|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_LogFilePath.Text = dlg.FileName;
            }
        }

        private void btn_BrowseErr_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Err files(*.err)|*.err|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_ErrorFilePath.Text = dlg.FileName;
            }
        }

        private void TableDataExtractorEditor_Load(object sender, EventArgs e)
        {
            
        }

        #region IComponentEditor Members

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get
            {
                return _ActivityNode;
            }
            set
            {
                _ActivityNode = value;
            }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(TableDataExtractorComponent); }
        }

        public void LoadSettings(Common.Utilities.Dispatcher.GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._ExtractComponent = (TableDataExtractorComponent)wfComponent;

            this.num_RunID.Value = this._ExtractComponent.RunID;
            this.txt_FileListXmlFilePath.Text = this._ExtractComponent.FileListXmlFilePath;
            this.txt_TableScanFilePath.Text = this._ExtractComponent.TableScanXmlFilePath;
            this.txt_LayoutFilePath.Text = this._ExtractComponent.TableLayoutXmlFilePath;
            this.txt_OutputFolder.Text = this._ExtractComponent.TableExtractorOutputFolder;
            this.txt_LogFilePath.Text = this._ExtractComponent.LogFilePath;
            this.txt_ErrorFilePath.Text = this._ExtractComponent.ErrFilePath;
            this.num_ThreadPoolSize.Value = this._ExtractComponent.ThreadPoolSize;
            this.txt_ConnStr.Text = this._ExtractComponent.ConnStr;

            this.cbo_DocStatusTable.Items.Clear();
            this.cbo_DocRecordTable.Items.Clear();
            if (!string.IsNullOrEmpty(this._ExtractComponent.ConnStr))
            {
                string[] tblNames = DbUtil.GetTableNames(DataProviderType.MSSQL, string.Empty, this._ExtractComponent.ConnStr);
                if (tblNames != null && tblNames.Length > 0)
                {
                    foreach (string tblname in tblNames)
                    {
                        this.cbo_DocStatusTable.Items.Add(tblname);
                        this.cbo_DocRecordTable.Items.Add(tblname);
                    }
                }
            }
            if (this.cbo_DocStatusTable.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.DocStatusOutputTable)))
            {
                for (int i = 0; i < this.cbo_DocStatusTable.Items.Count; i++)
                {
                    if (this.cbo_DocStatusTable.Items[i].ToString() == this._ExtractComponent.DocStatusOutputTable)
                    {
                        this.cbo_DocStatusTable.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_DocRecordTable.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.DocRecordOutputTable)))
            {
                for (int i = 0; i < this.cbo_DocRecordTable.Items.Count; i++)
                {
                    if (this.cbo_DocRecordTable.Items[i].ToString() == this._ExtractComponent.DocRecordOutputTable)
                    {
                        this.cbo_DocRecordTable.SelectedIndex = i;
                        break;
                    }
                }
            }
            this.cbo_DocID.Items.Clear();
            this.cbo_RunIDField.Items.Clear();
            this.cbo_IsElectronicField.Items.Clear();
            this.cbo_IsScannedField.Items.Clear();
            this.cbo_PageField.Items.Clear();
            this.cbo_RecCountField.Items.Clear();
            this.cbo_StatusCodeField.Items.Clear();
            this.cbo_CommentsField.Items.Clear();

           
            if (!string.IsNullOrEmpty(this._ExtractComponent.ConnStr) && !string.IsNullOrEmpty(this._ExtractComponent.DocStatusOutputTable))
            {
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, this._ExtractComponent.ConnStr, string.Empty, this._ExtractComponent.DocStatusOutputTable);
                if (cols != null && cols.Length > 0)
                {
                    foreach (TableColumn col in cols)
                    {
                        this.cbo_DocID.Items.Add(col.ColumnName);
                        this.cbo_RunIDField.Items.Add(col.ColumnName);
                        this.cbo_IsElectronicField.Items.Add(col.ColumnName);
                        this.cbo_IsScannedField.Items.Add(col.ColumnName);
                        this.cbo_PageField.Items.Add(col.ColumnName);
                        this.cbo_RecCountField.Items.Add(col.ColumnName);
                        this.cbo_StatusCodeField.Items.Add(col.ColumnName);
                        this.cbo_CommentsField.Items.Add(col.ColumnName);
                    }
                }
            }
            if (this.cbo_DocID.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.DocIDField)))
            {
                for (int i = 0; i < this.cbo_DocID.Items.Count; i++)
                {
                    if (this.cbo_DocID.Items[i].ToString() == this._ExtractComponent.DocIDField)
                    {
                        this.cbo_DocID.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_RunIDField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.RunIDField)))
            {
                for (int i = 0; i < this.cbo_DocID.Items.Count; i++)
                {
                    if (this.cbo_RunIDField.Items[i].ToString() == this._ExtractComponent.RunIDField)
                    {
                        this.cbo_RunIDField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_IsElectronicField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.IsElectronicField)))
            {
                for (int i = 0; i < this.cbo_IsElectronicField.Items.Count; i++)
                {
                    if (this.cbo_IsElectronicField.Items[i].ToString() == this._ExtractComponent.IsElectronicField)
                    {
                        this.cbo_IsElectronicField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_IsScannedField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.IsScannedField)))
            {
                for (int i = 0; i < this.cbo_IsScannedField.Items.Count; i++)
                {
                    if (this.cbo_IsScannedField.Items[i].ToString() == this._ExtractComponent.IsScannedField)
                    {
                        this.cbo_IsScannedField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_PageField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.PagesField)))
            {
                for (int i = 0; i < this.cbo_PageField.Items.Count; i++)
                {
                    if (this.cbo_PageField.Items[i].ToString() == this._ExtractComponent.PagesField)
                    {
                        this.cbo_PageField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_RecCountField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.RecordCountField)))
            {
                for (int i = 0; i < this.cbo_RecCountField.Items.Count; i++)
                {
                    if (this.cbo_RecCountField.Items[i].ToString() == this._ExtractComponent.RecordCountField)
                    {
                        this.cbo_RecCountField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_StatusCodeField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.DocFlagField)))
            {
                for (int i = 0; i < this.cbo_StatusCodeField.Items.Count; i++)
                {
                    if (this.cbo_StatusCodeField.Items[i].ToString() == this._ExtractComponent.DocFlagField)
                    {
                        this.cbo_StatusCodeField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_CommentsField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.CommentsField)))
            {
                for (int i = 0; i < this.cbo_CommentsField.Items.Count; i++)
                {
                    if (this.cbo_CommentsField.Items[i].ToString() == this._ExtractComponent.CommentsField)
                    {
                        this.cbo_CommentsField.SelectedIndex = i;
                        break;
                    }
                }
            }

            this.dv_FieldMappings.Rows.Clear();
            TableColumn[] tgtCols = null;
            if (!string.IsNullOrEmpty(this._ExtractComponent.ConnStr) && !string.IsNullOrEmpty(this._ExtractComponent.DocRecordOutputTable))
            {
                tgtCols = DbUtil.GetTableColumns(DataProviderType.MSSQL, this._ExtractComponent.ConnStr, string.Empty, this._ExtractComponent.DocRecordOutputTable);
            }
            if (this._ExtractComponent.SrcTgtFieldMappings != null && this._ExtractComponent.SrcTgtFieldMappings.Count > 0)
            {
                foreach (string srcFieldName in this._ExtractComponent.SrcTgtFieldMappings.Keys)
                {
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewTextBoxCell cellSrc = new DataGridViewTextBoxCell();
                    cellSrc.Value = srcFieldName;
                    dr.Cells.Add(cellSrc);
                    DataGridViewComboBoxCell cellTgt = new DataGridViewComboBoxCell();
                    if (tgtCols != null && tgtCols.Length > 0)
                    {
                        foreach (TableColumn col in tgtCols)
                        {
                            cellTgt.Items.Add(col.ColumnName);
                        }
                    }
                    cellTgt.Value = this._ExtractComponent.SrcTgtFieldMappings[srcFieldName];
                    dr.Cells.Add(cellTgt);
                    this.dv_FieldMappings.Rows.Add(dr);
                }
            }
            else
            {
                foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
                {
                    if (dr.IsNewRow)
                    {
                        DataGridViewComboBoxCell cellTgt = (DataGridViewComboBoxCell)dr.Cells[1];
                        if (tgtCols != null && tgtCols.Length > 0)
                        {
                            foreach (TableColumn col in tgtCols)
                            {
                                cellTgt.Items.Add(col.ColumnName);
                            }
                        }
                    }
                }
            }
        }

        public void SaveSettings()
        {
            this._ExtractComponent.RunID = (int) this.num_RunID.Value;
            this._ExtractComponent.FileListXmlFilePath = this.txt_FileListXmlFilePath.Text;
            this._ExtractComponent.TableScanXmlFilePath = this.txt_TableScanFilePath.Text;
            this._ExtractComponent.TableLayoutXmlFilePath = this.txt_LayoutFilePath.Text;
            this._ExtractComponent.TableExtractorOutputFolder = this.txt_OutputFolder.Text;
            this._ExtractComponent.LogFilePath = this.txt_LogFilePath.Text;
            this._ExtractComponent.ErrFilePath = this.txt_ErrorFilePath.Text;

            this._ExtractComponent.ThreadPoolSize = (int)this.num_ThreadPoolSize.Value;
            this._ExtractComponent.ConnStr = this.txt_ConnStr.Text;
            this._ExtractComponent.DocStatusOutputTable = this.cbo_DocStatusTable.Text;
            this._ExtractComponent.DocRecordOutputTable = this.cbo_DocRecordTable.Text;
            this._ExtractComponent.DocIDField = this.cbo_DocID.Text;
            this._ExtractComponent.RunIDField = this.cbo_RunIDField.Text;
            this._ExtractComponent.IsElectronicField = this.cbo_IsElectronicField.Text;
            this._ExtractComponent.IsScannedField = this.cbo_IsScannedField.Text;
            this._ExtractComponent.PagesField = this.cbo_PageField.Text;
            this._ExtractComponent.RecordCountField = this.cbo_RecCountField.Text;
            this._ExtractComponent.DocFlagField = this.cbo_StatusCodeField.Text;
            this._ExtractComponent.CommentsField = this.cbo_CommentsField.Text;

            this._ExtractComponent.SrcTgtFieldMappings = new Dictionary<string, string>();
            foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
            {
                if (!dr.IsNewRow && dr.Cells[0].Value != null && dr.Cells[1].Value != null)
                {
                    string srcField = dr.Cells[0].Value.ToString();
                    string tgtField = dr.Cells[1].Value.ToString();
                    this._ExtractComponent.SrcTgtFieldMappings.Add(srcField, tgtField);
                }
            }
        }

        public TestResult Test()
        {
            this.SaveSettings();
            return new TestResult(true, "Success");
        }

        #endregion

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if (this.UpdateComponentSettingEvent != null)
            {
                this.UpdateComponentSettingEvent(this._ActivityNode, this._ExtractComponent);
            }

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dv_FieldMappings_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
            {
                if (dr.IsNewRow)
                {
                    DataGridViewComboBoxCell cellTgtField = (DataGridViewComboBoxCell)dr.Cells[1];
                    if (!string.IsNullOrEmpty(this.txt_ConnStr.Text) && !string.IsNullOrEmpty(this.cbo_DocRecordTable.Text))
                    {
                        TableColumn[] cols = DbUtil.GetTableColumns(
                            DataProviderType.MSSQL, this.txt_ConnStr.Text,
                            string.Empty, this.cbo_DocRecordTable.Text);
                        if (cols != null && cols.Length > 0)
                        {
                            foreach (TableColumn col in cols)
                            {
                                cellTgtField.Items.Add(col.ColumnName);
                            }
                        }
                    }
                }
            }
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            this.cbo_DocStatusTable.Items.Clear();
            this.cbo_DocRecordTable.Items.Clear();
            if (!string.IsNullOrEmpty(this.txt_ConnStr.Text))
            {
                string[] tblNames = DbUtil.GetTableNames(DataProviderType.MSSQL, string.Empty, this.txt_ConnStr.Text);
                if (tblNames != null && tblNames.Length > 0)
                {
                    foreach (string tblname in tblNames)
                    {
                        this.cbo_DocStatusTable.Items.Add(tblname);
                        this.cbo_DocRecordTable.Items.Add(tblname);
                    }
                }
            }
            if (this.cbo_DocStatusTable.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.DocStatusOutputTable)))
            {
                for (int i = 0; i < this.cbo_DocStatusTable.Items.Count; i++)
                {
                    if (this.cbo_DocStatusTable.Items[i].ToString() == this._ExtractComponent.DocStatusOutputTable)
                    {
                        this.cbo_DocStatusTable.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_DocStatusTable.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.DocRecordOutputTable)))
            {
                for (int i = 0; i < this.cbo_DocRecordTable.Items.Count; i++)
                {
                    if (this.cbo_DocRecordTable.Items[i].ToString() == this._ExtractComponent.DocRecordOutputTable)
                    {
                        this.cbo_DocRecordTable.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void cbo_TgtTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbo_DocStatusTable.SelectedIndex >= 0 && (!string.IsNullOrEmpty(this.txt_ConnStr.Text)))
            {
                this.cbo_DocID.Items.Clear();
                this.cbo_RunIDField.Items.Clear();
                this.cbo_IsElectronicField.Items.Clear();
                this.cbo_IsScannedField.Items.Clear();
                this.cbo_PageField.Items.Clear();
                this.cbo_RecCountField.Items.Clear();
                this.cbo_StatusCodeField.Items.Clear();
                this.cbo_CommentsField.Items.Clear();

                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL,
                    this.txt_ConnStr.Text, string.Empty,
                    this.cbo_DocStatusTable.SelectedItem.ToString());
                if (cols != null && cols.Length > 0)
                {
                    foreach (TableColumn col in cols)
                    {
                        this.cbo_DocID.Items.Add(col.ColumnName);
                        this.cbo_RunIDField.Items.Add(col.ColumnName);
                        this.cbo_IsElectronicField.Items.Add(col.ColumnName);
                        this.cbo_IsScannedField.Items.Add(col.ColumnName);
                        this.cbo_PageField.Items.Add(col.ColumnName);
                        this.cbo_RecCountField.Items.Add(col.ColumnName);
                        this.cbo_StatusCodeField.Items.Add(col.ColumnName);
                        this.cbo_CommentsField.Items.Add(col.ColumnName);
                    }
                }
            }
            if (this.cbo_DocID.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.DocIDField)))
            {
                for (int i = 0; i < this.cbo_DocID.Items.Count; i++)
                {
                    if (this.cbo_DocID.Items[i].ToString() == this._ExtractComponent.DocIDField)
                    {
                        this.cbo_DocID.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_RunIDField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.RunIDField)))
            {
                for (int i = 0; i < this.cbo_DocID.Items.Count; i++)
                {
                    if (this.cbo_RunIDField.Items[i].ToString() == this._ExtractComponent.RunIDField)
                    {
                        this.cbo_RunIDField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_IsElectronicField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.IsElectronicField)))
            {
                for (int i = 0; i < this.cbo_IsElectronicField.Items.Count; i++)
                {
                    if (this.cbo_IsElectronicField.Items[i].ToString() == this._ExtractComponent.IsElectronicField)
                    {
                        this.cbo_IsElectronicField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_IsScannedField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.IsScannedField)))
            {
                for (int i = 0; i < this.cbo_IsScannedField.Items.Count; i++)
                {
                    if (this.cbo_IsScannedField.Items[i].ToString() == this._ExtractComponent.IsScannedField)
                    {
                        this.cbo_IsScannedField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_PageField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.PagesField)))
            {
                for (int i = 0; i < this.cbo_PageField.Items.Count; i++)
                {
                    if (this.cbo_PageField.Items[i].ToString() == this._ExtractComponent.PagesField)
                    {
                        this.cbo_PageField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_RecCountField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.RecordCountField)))
            {
                for (int i = 0; i < this.cbo_RecCountField.Items.Count; i++)
                {
                    if (this.cbo_RecCountField.Items[i].ToString() == this._ExtractComponent.RecordCountField)
                    {
                        this.cbo_RecCountField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_StatusCodeField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.DocFlagField)))
            {
                for (int i = 0; i < this.cbo_StatusCodeField.Items.Count; i++)
                {
                    if (this.cbo_StatusCodeField.Items[i].ToString() == this._ExtractComponent.DocFlagField)
                    {
                        this.cbo_StatusCodeField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_CommentsField.Items.Count > 0 && (!string.IsNullOrEmpty(this._ExtractComponent.CommentsField)))
            {
                for (int i = 0; i < this.cbo_CommentsField.Items.Count; i++)
                {
                    if (this.cbo_CommentsField.Items[i].ToString() == this._ExtractComponent.CommentsField)
                    {
                        this.cbo_CommentsField.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        private void cbo_DocRecordTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbo_DocRecordTable.SelectedIndex >= 0 && (!string.IsNullOrEmpty(this.txt_ConnStr.Text)))
            {
                
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL,
                    this.txt_ConnStr.Text, string.Empty,
                    this.cbo_DocRecordTable.SelectedItem.ToString());
                if (cols != null && cols.Length > 0)
                {
                    foreach (DataGridViewRow dr in this.dv_FieldMappings.Rows)
                    {
                        if (dr.IsNewRow)
                        {
                            DataGridViewComboBoxCell cellTgtField = (DataGridViewComboBoxCell)dr.Cells[1];
                            foreach (TableColumn col in cols)
                            {
                                cellTgtField.Items.Add(col.ColumnName);
                            }
                        }
                    }
                }

                
            }
        }

        private void btn_BrowseScanOutput_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Xml files(*.xml)|*.xml|All files(*.*)|*.*";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.txt_TableScanFilePath.Text = dlg.FileName;
            }
        }

    }
}
